﻿using BasicModuleOrm.Db_Context;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BasicModuleOrm.Model;
using BasicModuleOrm.ProductRespository.Respository;

namespace BasicModuleOrm.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        public IRespository<User> _Respository { get; set; }
        public UserController(IRespository<User> respository) 
        {
            this._Respository = respository;
        }

        /// <summary>
        /// 查询所有商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IEnumerable<User> GetUsers()
        {
            return _Respository.GetAll();
        }


        /// <summary>
        /// 查询商品根据商品Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<User> GetUser(int id)
        {
            // 1、查询商品
            var User = _Respository.Get(id);

            return User;
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="User"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<User> PostUser(User User)
        {
            // 1、添加商品
            _Respository.Add(User);

            return CreatedAtAction(nameof(PostUser), new { id = User.Id }, User);
        }


        /// <summary>
        /// 更新商品通过Id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="User"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public IActionResult PutUser(int id, User User)
        {
            _Respository.Update(id, User);

            return Ok("更新成功");
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IActionResult DeleteUser(int id)
        {
            _Respository.Delete(id);

            return Ok("删除成功");
        }
    }
}
